package com.sqly.doc.user;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.IdRes;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.ble.xty.WdBleFindActivity;
import com.sqly.doc.pojo.BleEquip;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.NetWorkUtils;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.StringUtils;

import cz.msebera.android.httpclient.Header;

/**
 * 服务包新增或编辑界面
 * Created by Administrator on 2017/11/18 0018.
 */

public class bleequip_add extends MyBaseActivity{
    //标题
    TextView title, uname, sbname, sbmac;
    EditText sbsn;
    BleEquip obj;
    Context con = bleequip_add.this;
    String url = HttpUrl.SaveBleEquip;
    RadioGroup sblx;

    @Override
    protected void initUI() {
        setContentView(R.layout.bleequip_add);
        init();
    }

    private void init(){
        //标题
        title = (TextView) findViewById(R.id.title);
        uname = (TextView) findViewById(R.id.uname);
        sbname = (TextView) findViewById(R.id.sbname);
        sbsn = (EditText)findViewById(R.id.sbsn);
        sbname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(con, WdBleFindActivity.class), 1);
            }
        });
        sbmac = (TextView) findViewById(R.id.sbmac);
        title.setText("蓝牙设备绑定");
        //性别选择
        sblx = (RadioGroup) findViewById(R.id.sblx);
        if (obj != null && !StringUtils.isEmpty(obj.getCategory())) {
            switch (Integer.parseInt(obj.getCategory())) {
                case 1:
                    sblx.check(R.id.sblx_xty);
                    break;
                case 2:
                    sblx.check(R.id.sblx_xyj);
                    break;
            }
        } else {
            sblx.check(R.id.sblx_xty);
        }
        if (obj == null) {
            obj = new BleEquip();
            obj.setCategory("1");
        }
        sblx.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.sblx_xty == checkedId) {
                    obj.setCategory("1");
                }
                if (R.id.sblx_xyj == checkedId)
                    obj.setCategory("2");
            }
        });
        uname.setText("保存");
        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != sbname && StringUtils.isEmpty(sbname.getText().toString())){
                    MyToastUtil.ShowToast(con, "设备名称不能为空，请点击选择蓝牙设备");
                }else if(null != sbmac && StringUtils.isEmpty(sbmac.getText().toString())){
                    MyToastUtil.ShowToast(con, "设备MAC不能为空，请点击选择蓝牙设备");
                }else{
                    obj.setName(sbname.getText().toString());
                    obj.setMac(sbmac.getText().toString());
                    obj.setSn(sbsn.getText().toString());

                    if (NetWorkUtils.isNetworkConnected(con)) {///网络连接
                        RequestParams ps = new RequestParams();
                        ps.add("entity.name", obj.getName());
                        ps.add("entity.mac", obj.getMac());
                        ps.add("entity.sn", obj.getSn());
                        Log.i("category","========"+obj.getCategory());
                        ps.add("entity.category", obj.getCategory());
                        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                        ps.add("type","2");

                        UserClient.get(url, ps, new AsyncHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                                String content = new String(response);
                                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                                if (ret.getCode().equals("200")) {
                                    obj.save();
                                    if(null !=obj.getCategory() && "1".equals(obj.getCategory())) {
                                        SharedPreferencesUtils.setParam(con, "xtyval", obj.getMac());
                                        if(null == SharedPreferencesUtils.getParam(con, "xyjval", "") || "".equals(SharedPreferencesUtils.getParam(con, "xyjval", ""))){
                                            SharedPreferencesUtils.setParam(con, "xyjval", "");
                                        }
                                    }else{
                                        SharedPreferencesUtils.setParam(con, "xyjval", obj.getMac());
                                        if(null == SharedPreferencesUtils.getParam(con, "xtyval", "") || "".equals(SharedPreferencesUtils.getParam(con, "xtyval", ""))){
                                            SharedPreferencesUtils.setParam(con, "xtyval", "");
                                        }
                                    }
                                    MyToastUtil.ShowToast(con, "成功");
                                }else{
                                    MyToastUtil.ShowToast(con, ret.getInfo());
                                }
                                finish();
                            }
                            @Override
                            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                                MyToastUtil.ShowToast(con, "数据传输失败");
                            }
                        });
                    } else {//网络没有链接
                        MyToastUtil.ShowToast(con, "请联网操作！");
                    }
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {

        } else {
            if (requestCode == 1) {
                BleEquip temp = (BleEquip) data.getExtras().getSerializable("bleEquip");
                sbname.setText(temp.getName());
                sbmac.setText(temp.getMac());
                sbsn.setText(temp.getSn());
            }
        }
    }
}
